<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>async and await</title>
</head>
<body>
<script>
    /**
     * async 返回的结果的promise的结果由函数执行结果决定
     * */
    async function fn1() {
        // return 1
        throw 1;
    }

    const result = fn1();
    result.then(value => {
        console.log(value)
    }, reason => {
        console.log(reason)
    })
    console.log(result)

    function fn2(){
        return Promise.resolve(2);
    }
    function fn4(){
        return 6;
    }
    function fn5(){
        throw 5;
    }
    /**
     * await必须在async中,
     * 抛出的异常由try catch捕获
     * */
    async function fn3(){
        // const value = await fn2(); // await 右侧为promise成功的value
        const value = await fn4(); //await 不为promise，返回的就是它本身
        console.log(value)
        try {
            const error = await fn5();
            console.log(error);
        }catch (error){
            console.log(error)
        }
    }
    fn3();
</script>
</body>
</html>